package indi.gao.redis;

import java.io.*;
import java.util.*;

/**
 * <p>Description:</p>
 * <p>Copyright: Copyright (c)2019$</p>
 * <p>Company: Tope</p>
 * <P>Created Date :2019$-08$-09$</P>
 * <P>@version 1.0</P>
 */
public class txt {
    public static void main(String[] args) throws IOException {
        List<String> studentIDsList = new ArrayList<>();
        Map<String, Object> UpdateDayTotalReadList = new HashMap<>();
        Map<String, Object> studentMap = new HashMap<>();
        List<String> result = new ArrayList<>();
        /* 读取数据 */

        BufferedReader studentIDs = new BufferedReader(new InputStreamReader(new FileInputStream(new File("F:\\workSpace\\UtilTest\\TxtReadAndWrite\\src\\main\\file\\studentID.txt")), "UTF-8"));
        BufferedReader UpdateDayTotalRead = new BufferedReader(new InputStreamReader(new FileInputStream(new File("F:\\workSpace\\UtilTest\\TxtReadAndWrite\\src\\main\\file\\UpdateDayTotalRead.txt")), "UTF-8"));
        String lineTxt = null;
        while ((lineTxt = studentIDs.readLine()) != null) {//读取每行的数据
            String[] stringsID = lineTxt.split(";");
            studentIDsList.addAll(Arrays.asList(stringsID));
        }
        while ((lineTxt = UpdateDayTotalRead.readLine()) != null) {//数据以逗号分隔
            String[] stringsRead = lineTxt.split(";");
            for (String string : stringsRead) {
                String studentID = getNum(string, "studentID = ");
                UpdateDayTotalReadList.put(studentID, string);
            }
        }
        studentIDs.close();
        UpdateDayTotalRead.close();

        for (String studentID : studentIDsList) {
            if (UpdateDayTotalReadList.containsKey(studentID.trim())) {
                String updateToInsert = (String) UpdateDayTotalReadList.get(studentID);
                studentMap = toMap(updateToInsert);
                Set<String> key = studentMap.keySet();
                String head = "insert into rd_day_total_read";
                String tableString = "";
                String valueString = "";
                for (String s : key) {
                    valueString = valueString + studentMap.get(s) + " , ";
                    tableString = tableString + s + " , ";
                }
                tableString = tableString.substring(0, tableString.lastIndexOf(","));
                valueString = valueString.substring(0, valueString.lastIndexOf(","));
                updateToInsert = head + " ( " + tableString + " ) " + "values ( " + valueString + " )";
                result.add(updateToInsert + " ;");
            }
        }

        /* 输出数据 */
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("E:/value_map.txt")),
                    "UTF-8"));

            for (String sqlString : result) {
                bw.write(sqlString);
                bw.newLine();
            }
            bw.close();
        } catch (Exception e) {
            System.err.println("write errors :" + e);
        }
    }

    private static Map<String, Object> toMap(String string) {
        Map<String, Object> stuMap = new HashMap<>();
        stuMap.put("readSecond", getNum(string, "readSecond = "));
        stuMap.put("readSentenceNum", getNum(string, "readSentenceNum = "));
        stuMap.put("readWordNum", getNum(string, "readWordNum = "));
        stuMap.put("skipSentenceNum", getNum(string, "skipSentenceNum = "));
        stuMap.put("skipWordNum", getNum(string, "skipWordNum = "));
        stuMap.put("readBook", getNum(string, "readBook = "));
        stuMap.put("studentID", getNum(string, "studentID = "));
        stuMap.put("readDay", getNum(string, "readDay = "));
        return stuMap;
    }

    private static String getNum(String string, String find) {
        int begin = string.lastIndexOf(find) + find.length();
        int end = begin;
        while (end < string.length() && (((string.charAt(end) <= '9') && (string.charAt(end) >= '0')) || (string.charAt(end) == '\'') || string.charAt(end) == '-')) {
            end++;
        }
        return string.substring(begin, end).trim();
    }
}
